package de.s9mtdev.lol_destiny;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Home extends Activity {
	
	EditText edit1;
	EditText edit2;
	Button button1;
	Button button2;	
	boolean online = false;
	Messenger messenger = null;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			Bundle b = msg.getData();
			if (b.getString("info") != null && b.getString("info").equals("online")) {
				online = true;
				TextView t = (TextView)findViewById(R.id.textView2);
				t.setText(R.string.irc_connected);
				ProgressBar p = (ProgressBar)findViewById(R.id.progressBar1);
				p.setVisibility(View.INVISIBLE);
				updateButtonState();
			}
			else if (b.getString("info") != null && b.getString("info").equals("offline")) {
				online = false;
				// try to reconnect
				// do visual stuff
			}				
		}
	};
	
	private ServiceConnection conn = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder binder) {
            messenger = new Messenger(binder);
        }
        public void onServiceDisconnected(ComponentName className) {
            messenger = null;
        }
	};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_home); 
        
        Intent service = new Intent(this, NetworkService.class);
        Messenger m = new Messenger(handler);
        service.putExtra("MESSENGER", m);
        bindService(service, conn, Context.BIND_AUTO_CREATE);
        
        edit1 = (EditText)findViewById(R.id.editText1);
        edit2 = (EditText)findViewById(R.id.editText2);
        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);
        
        TextWatcher watcher = new LocalTextWatcher();
        edit1.addTextChangedListener(watcher);
        edit2.addTextChangedListener(watcher);
        updateButtonState();        
    }
    
    void updateButtonState() {
		button1.setEnabled(online);
        boolean enabled = checkEditText(edit1.getText().toString(), edit2.getText().toString())
            && online;
        button2.setEnabled(enabled);
    }
        
    private boolean checkEditText(String key, String pass) {
        return key.length() == 5 && pass.length() == 3;
    }
    
    private class LocalTextWatcher implements TextWatcher {
        public void afterTextChanged(Editable s) {
            updateButtonState();
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
    }
            
    @Override
    public void onDestroy() {
    	super.onDestroy();
    	unbindService(conn);    	
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.layout_home, menu);
        return true;
    }
    
    /** Called when the user clicks the HOST button */
    public void hostSession(View view) {
    	String sessionkey = Utils.generateRnd(5);
    	String sessionpass = Utils.generateRnd(3);
    	Message msg = Message.obtain();
		try {
			Bundle bundle = new Bundle();
			bundle.putString("session", sessionkey +":"+ sessionpass);
			msg.setData(bundle);
			messenger.send(msg);
		} catch (RemoteException e1) {e1.printStackTrace();}
        Intent intent = new Intent(this, InSession.class);
        intent.putExtra("session", sessionkey +":"+ sessionpass);
        startActivity(intent);
    }
    
    /** Called when the user clicks the JOIN button */
    public void joinSession(View view) {
    	String sessionkey = ((EditText)findViewById(R.id.editText1)).getText().toString();
    	String sessionpass = ((EditText)findViewById(R.id.editText2)).getText().toString();
    	Message msg = Message.obtain();
		try {
			Bundle bundle = new Bundle();
			bundle.putString("session", sessionkey +":"+ sessionpass);
			msg.setData(bundle);
			messenger.send(msg);
		} catch (RemoteException e1) {e1.printStackTrace();}
        Intent intent = new Intent(this, InSession.class);
        intent.putExtra("session", sessionkey +":"+ sessionpass);
        startActivity(intent);
    }
}
